package com.example.pclimb.ui.guides;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;

import com.example.pclimb.R;
import com.example.pclimb.databinding.ActivityCreateGuideBinding;
import com.example.pclimb.model.TravelGuide;
import com.example.pclimb.util.SharedPreferencesUtil;
import com.example.pclimb.viewmodel.TravelGuideViewModel;

import java.util.Date;

/**
 * 创建旅游攻略的活动
 */
public class CreateGuideActivity extends AppCompatActivity {

    private ActivityCreateGuideBinding binding;
    private TravelGuideViewModel viewModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityCreateGuideBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        // 设置工具栏
        setSupportActionBar(binding.toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        binding.toolbar.setNavigationOnClickListener(v -> onBackPressed());

        // 初始化ViewModel
        viewModel = new ViewModelProvider(this).get(TravelGuideViewModel.class);

        // 设置提交按钮点击事件
        binding.btnSubmit.setOnClickListener(v -> submitGuide());
    }

    /**
     * 提交攻略
     */
    private void submitGuide() {
        // 重置错误提示
        binding.tilTitle.setError(null);
        binding.tilLocation.setError(null);
        binding.tilContent.setError(null);

        // 获取输入值
        String title = binding.etTitle.getText().toString().trim();
        String location = binding.etLocation.getText().toString().trim();
        String content = binding.etContent.getText().toString().trim();

        // 验证输入
        boolean cancel = false;
        View focusView = null;

        if (TextUtils.isEmpty(content)) {
            binding.tilContent.setError(getString(R.string.error_field_required));
            focusView = binding.etContent;
            cancel = true;
        }

        if (TextUtils.isEmpty(location)) {
            binding.tilLocation.setError(getString(R.string.error_field_required));
            focusView = binding.etLocation;
            cancel = true;
        }

        if (TextUtils.isEmpty(title)) {
            binding.tilTitle.setError(getString(R.string.error_field_required));
            focusView = binding.etTitle;
            cancel = true;
        }

        if (cancel) {
            // 输入有误，聚焦到错误输入框
            focusView.requestFocus();
        } else {
            // 显示进度条
            showProgress(true);

            // 创建攻略
            int userId = SharedPreferencesUtil.getUserId(this);
            Date publishDate = new Date(); // 当前日期为发布日期

            TravelGuide guide = new TravelGuide();
            guide.setTitle(title);
            guide.setLocation(location);
            guide.setContent(content);
            guide.setAuthorId(userId);
            guide.setPublishDate(publishDate);
            guide.setViewCount(0);

            // 提交攻略
            viewModel.insert(guide, id -> {
                runOnUiThread(() -> {
                    showProgress(false);
                    if (id > 0) {
                        // 提交成功
                        Toast.makeText(CreateGuideActivity.this, R.string.guide_submitted, Toast.LENGTH_SHORT).show();
                        finish();
                    } else {
                        // 提交失败
                        Toast.makeText(CreateGuideActivity.this, R.string.guide_submission_failed, Toast.LENGTH_SHORT).show();
                    }
                });
            });
        }
    }

    /**
     * 显示或隐藏进度条
     */
    private void showProgress(boolean show) {
        binding.progressBar.setVisibility(show ? View.VISIBLE : View.GONE);
        binding.btnSubmit.setEnabled(!show);
        binding.etTitle.setEnabled(!show);
        binding.etLocation.setEnabled(!show);
        binding.etContent.setEnabled(!show);
    }
} 